package com.kn.knsqlitegenerator.files;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ImportSQLiteFiles
{

	public static List<String> run()
	{
		List<String> listDatabases = new ArrayList<String>();
		File folderDatabases = new File("Generator/Import");

		if (folderDatabases.exists() == false)
		{
			folderDatabases.mkdirs();
		}

		String[] files = folderDatabases.list();

		for (String file : files)
		{
			if (file.endsWith(".sqlite") == true)
			{
				listDatabases.add(file);

				String folderName = file.replace(".sqlite", "");
				String pathFolder = "Generator/Export/" + folderName;

				deleteFolder(new File(pathFolder));
				createFolder(pathFolder + "/BLL");
				createFolder(pathFolder + "/DATA");
				createFolder(pathFolder + "/DAL");

			}
		}

		return listDatabases;
	}

	private static void createFolder(String path)
	{
		File folder = new File(path);

		folder.mkdirs();
	}

	private static void deleteFolder(File folder)
	{
		File[] files = folder.listFiles();
		if (files != null)
		{
			for (File f : files)
			{
				if (f.isDirectory())
				{
					deleteFolder(f);
				}
				else
				{
					f.delete();
				}
			}
		}
		folder.delete();
	}
}
